import cookies from 'js-cookie'
import ManageConstant from "../consts/manageConstant";
import qs from "qs"

class VideoService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        Token: cookies.get('Token')
      }
    });
  }

  searchVideo(params, executeMethod){
    this._axios.post(ManageConstant.VIDEO_SEARCH, params).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  uploadVideo(params, executeMethod){
    this._axios.post(ManageConstant.VIDEO_UPLOAD, params,{headers: { "Content-Type": "multipart/form-data" }}).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
      else if(res.data.resultCode == 1013){
        this._this.$Message.error("视频标题已存在");
      }
      else if(res.data.resultCode == 1014){
        this._this.$Message.error("请不要重复上传文件名相同的文件");
      }
    })
  }

  videoAudit(params, executeMethod){
    this._axios.post(ManageConstant.VIDEO_AUDIT, qs.stringify(params, {indices: false})
    ).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  batchDeleteVideos(videoIds, executeMethod){
    this._axios.post(ManageConstant.VIDEO_BATCH_DELETE, qs.stringify({videoIds}, {indices: false})
    ).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

}

export default VideoService;
